/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package core;

import java.util.ArrayList;
import java.util.List;
import model.Department;
import model.Employee;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import util.ProjetHibernateUtil;

/**
 *
 * @author phinguyen
 */
public class StepOwnerCore {

    /**
     * store the hibernate session for the step owner core
     */
    private static Session session;

    public StepOwnerCore() {
        session = ProjetHibernateUtil.getSessionFactory().openSession();
    }

    /**
     * creation of a new employee and attribution to a Department
     *
     * @param department
     * @param name
     * @param firstname
     * @param email
     * @param pwd
     */
    public void addEmployee(Department department, String name, String firstname, String email, String pwd) {
        Employee employee = new Employee(department, name, firstname, email, pwd);
        Transaction tr = session.beginTransaction();
        session.saveOrUpdate(employee);
        tr.commit();
    }

    /**
     * move an employee from a department to an other
     *
     * @param department
     * @param employee
     */
    public void moveEmployee(Department department, Employee employee) {
        employee.setDepartment(department);
        Transaction tr = session.beginTransaction();
        session.saveOrUpdate(employee);
        tr.commit();
    }

    public Department researchDepartmentById(String id) {
        return (Department) session.get(Department.class, id);
    }

    public Department researchDepartmentByName(String name) {
        for (Department department : this.getAllDepartment()) {
            if (department.getDeptname().equals(name)) {
                return department;
            }
        }
        return null;
    }

    public List<Employee> getAllEmployeesOfDepartmentResearchedByName(String department) {
        return getAllEmployeesOfDepartment(researchDepartmentByName(department));
    }

    /**
     * return all departments. It will call an HQL query in Hibernate
     * Department's mapping
     *
     * @return
     */
    public List<Department> getAllDepartment() {
        Query query = session.getNamedQuery("HQL_ALL_DEPARTMENT");
        List<Department> listAllDepartment = query.list();
        return listAllDepartment;
    }

    /**
     * return all employees of a department
     *
     * @param department
     * @return
     */
    public List<Employee> getAllEmployeesOfDepartment(Department department) {
        List<Employee> lstResult = new ArrayList();
        for (Employee employee : this.getAllEmployees()) {
            if (employee.getDepartment().equals(department)) {
                lstResult.add(employee);
            }
        }
        return lstResult;
    }

    public List<Employee> getAllEmployees() {
        Query query = session.getNamedQuery("HQL_ALL_EMPLOYEE");
        List result = query.list();
        return result;
    }

    /**
     * return all emails of employees
     *
     * @return
     */
    public List<String> getAllEmailEmployee() {
        Query query = session.getNamedQuery("HQL_ALL_EMAILEMPLOYEE");
        List result = query.list();
        return result;
    }

    public Employee getEmployeeByMail(String mail) {
        for (Employee employee : this.getAllEmployees()) {
            if (employee.getEmail().equals(mail)) {
                return employee;
            }
        }
        return null;
    }

    public void giveEmployeeValidationRights(String[] employees) {
        for (String idEmployee : employees) {
            Employee curentEmployee = this.getEmployeeById(idEmployee);
            curentEmployee.setEcanvalidate(true);
            Transaction tr = session.beginTransaction();
            session.saveOrUpdate(curentEmployee);
            tr.commit();
        }
    }

    public void giveEmployeeValidationRights(List<Employee> employees) {
        for (Employee employee : employees) {
            employee.setEcanvalidate(true);
            Transaction tr = session.beginTransaction();
            session.saveOrUpdate(employee);
            tr.commit();
        }
    }

    public void removeEmployeeValidationRights(List<Employee> employees) {
        for (Employee employee : employees) {
            employee.setEcanvalidate(false);
            Transaction tr = session.beginTransaction();
            session.saveOrUpdate(employee);
            tr.commit();
        }
    }

    private Employee getEmployeeById(String idEmployee) {
        for (Employee employee : this.getAllEmployees()) {
            if (employee.getEid().toString().equals(idEmployee)) {
                return employee;
            }
        }
        return null;
    }

    public void revokeEmployeeValidationRights(String[] idEmployees) {
        for (String idEmployee : idEmployees) {
            Employee curentEmployee = this.getEmployeeById(idEmployee);
            curentEmployee.setEcanvalidate(false);
            Transaction tr = session.beginTransaction();
            session.saveOrUpdate(curentEmployee);
            tr.commit();
        }
    }

    public void deleteEmployee(String[] idEmployees) {
         for (String idEmployee : idEmployees) {
            Employee curentEmployee = this.getEmployeeById(idEmployee);
            Transaction tr = session.beginTransaction();
            session.delete(curentEmployee);
            tr.commit();
        }
    }
}
